package cz.kfad.ofrss.db;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class AbstractDatabaseHelper {

    private final DatabaseOpenHelper databaseOpenHelper;

    public AbstractDatabaseHelper(Context context) {
        this.databaseOpenHelper = new DatabaseOpenHelper(context);
    }

    public interface ActionWithDatabase {
        void doAction(SQLiteDatabase database);
    }

    public interface ActionWithResultWithDatabase {
        <T> T doAction(SQLiteDatabase database);
    }

    protected void doWithWritableDatabase(ActionWithDatabase action) {
        final SQLiteDatabase writableDatabase = databaseOpenHelper.getWritableDatabase();
        action.doAction(writableDatabase);
        writableDatabase.close();
    }

    protected <T> T doWithReadableDatabase(ActionWithResultWithDatabase action) {
        final SQLiteDatabase readableDatabase = databaseOpenHelper.getReadableDatabase();
        final T result = action.doAction(readableDatabase);
        readableDatabase.close();
        return result;
    }


}
